import utils from '../utils'

function compileExpressionToFunction(exp, opts) {
  // $$ added to avoid duplication with data field variables (an error condition)
  const functionBody = `with($$env){with($$record){ ${opts.returns ? 'return ' : ''}${exp}}}`
  let func = null
  try {
    func = new Function("$$record,$$env",  functionBody)
  } catch(e) {
    // stop(e.name, "in expression [" + exp + "]")
    utils.error(`${e.name}in expression [${exp}]`)
  }
  return func
}
function getBaseContext() {
  const obj = {}
  // Mask global properties (is this effective/worth doing?)
  (function() {
    for (let key in this) {
      obj[key] = void 0
    }
  }())
  obj.console = console
  return obj
}
export default {
  compileExpressionToFunction,
  getBaseContext,
}